package com.ssm.controller;

import com.ssm.pojo.Customer;
import com.ssm.pojo.Enter;
import com.ssm.pojo.House;
import com.ssm.service.CustomerService;
import com.ssm.service.EnterService;
import com.ssm.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.CustomSQLExceptionTranslatorRegistrar;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/enter")
public class EnterController {
    @Autowired
    private HouseService houseService;
    @Autowired
    private EnterService enterService;
    @Autowired
    private CustomerService customerService;
    @RequestMapping("/hotel_checkin")
    public String hotel_checkin(ModelMap map){
        List<House> houseList=houseService.queryHouseByState("空闲");
        map.addAttribute("houseList",houseList);
        return "hotel_checkin";
    }
    @RequestMapping("/checkin")
    public void checkin(Enter enter,HttpServletResponse resp){
        				/*
	private Integer id;//不用传
    private Integer houseId;//传入id
    private Integer customerId;//不传入 controller中设置 后台查询
    private String customerInfo;//传入根据card查询 顾客id
    private Date startTime;//传入
    private Date endTimeEstimate;//传入
    private Date endTimeActual;//暂时不写入
    private String orderCustomerId;//不传入 controller中设置  根据card查询 顾客id 默认第一个card的id值
    private Double feeTotal;//传入
    private String feeInfo;//传入
    private Integer userId;//传入
    private String state;//不传入 controller中设置为 在住中
    private Double price;//传入price
				 */
        System.out.println("请求入住"+enter);
        //根据card查询出所有的入住人的id
        String orderCustomerId="";
        String customerId = "";
        String state="在住中";
        int j=0;
        for(int i=0;i<(enter.getCustomerInfo().length()/2);i++){

            Customer customer=customerService.queryCustomerByCard(enter.getCustomerInfo().substring(j,j+2));j+=2;
            if (i==0){
                orderCustomerId=Integer.toString(customer.getId());
            }
            customerId+=Integer.toString(customer.getId());
        }
        enter.setCustomerId(Integer.parseInt(customerId));
        enter.setOrderCustomerId(orderCustomerId);
        enter.setState(state);
        System.out.println("完整的入住信息"+enter);
        int n=enterService.insert(enter);
        try {
            PrintWriter out=resp.getWriter();
            out.print(n);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
